package com.best.onegous.Refresh;

import com.best.onegous.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class MeiTuanRefreshFirstStepView extends View{

	private Bitmap initialBitmap;
	private int measuredWidth;
	private int measuredHeight;
	private Bitmap endBitmap;
	private float mCurrentProgress;
	private Bitmap scaledBitmap;

	public MeiTuanRefreshFirstStepView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public MeiTuanRefreshFirstStepView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MeiTuanRefreshFirstStepView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		//��������Ǹ���Բ��ͼƬ
		initialBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pull_image));
		//����ǵڶ���״̬���޵�ͼƬ��֮����Ҫ����ͼƬ������Ϊ�ڶ���״̬�͵����״̬��ͼƬ�Ĵ�С��һ�µģ����һ�׶�
		//��Բ��ͼƬ�Ĵ�С��ڶ��׶κ͵���׶β�һ�£����������Ҫ�������ͼƬ��������һ��ͼƬ�Ŀ�ߣ�����֤
		//��һ�׶κ͵ڶ�����׶ε�View�Ŀ��һ��
		endBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pull_end_image_frame_01));//pull_end_image_frame_05
	}

	/**
	 * ��дonMeasure������Ҫ������wrap_contentʱ View�Ĵ�С
	 * @param widthMeasureSpec
	 * @param heightMeasureSpec
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		//������õĿ��������߶�  ����Ϊ��ϵڶ��׶�����ͼƬ�Ŀ�߱���
		setMeasuredDimension(measureWidth(widthMeasureSpec),measureWidth(widthMeasureSpec)*endBitmap.getHeight()/endBitmap.getWidth());
	}

	/**
	 * ��wrap_content��ʱ�򣬿�ȼ�Ϊ�ڶ��׶�����ͼƬ�Ŀ��
	 * @param widMeasureSpec
	 * @return
	 */
	private int measureWidth(int widMeasureSpec){
        int result = 0;
        int size = MeasureSpec.getSize(widMeasureSpec);
        int mode = MeasureSpec.getMode(widMeasureSpec);
        if (mode == MeasureSpec.EXACTLY){
            result = size;
        }else{
            result = endBitmap.getWidth();
            if (mode == MeasureSpec.AT_MOST){
                result = Math.min(result,size);
            }
        }
		return result;
        }

	/**
	 * ��onLayout�����ò�����View�Ŀ��
	 * @param changed
	 * @param left
	 * @param top
	 * @param right
	 * @param bottom
	 */
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		measuredWidth = getMeasuredWidth();
		measuredHeight = getMeasuredHeight();
		//��ݵڶ��׶����޿��  ����Բ��ͼƬ���еȱ��������
		scaledBitmap = Bitmap.createScaledBitmap(initialBitmap, measuredWidth,measuredWidth*initialBitmap.getHeight()/initialBitmap.getWidth(), true);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//��������ǶԻ����������ţ��Ӷ�ﵽ��Բ��ͼƬ�����ţ���һ������Ϊ������ű���ڶ�������Ϊ�߶����ű���
		canvas.scale(mCurrentProgress, mCurrentProgress, measuredWidth/2, measuredHeight/2);
		//���ȱ������ź����Բ�λ��ڻ�������
		canvas.drawBitmap(scaledBitmap,0,measuredHeight/4,null);
		
	}

	/**
	 * �������ű����0��1  0Ϊ��С 1Ϊ���
	 * @param currentProgress
	 */
	public void setCurrentProgress(float currentProgress){
		mCurrentProgress = currentProgress;
	}
	
	
}
